package com.sh.suhe.utildemos.views.activity.cathtml

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.sh.cathtml.adapter.CatalogueAdapter
import com.sh.suhe.utildemos.R
import com.sh.suhe.utildemos.model.MainModel
import com.sh.suhe.utildemos.utils.http.NetUtil
import kotlinx.android.synthetic.main.activity_catalogue.*
import org.jsoup.Jsoup

/**
 * 文章目录
 */
class CatalogueActivity : AppCompatActivity() ,CatalogueAdapter.OnClick{
    override fun toArticle(item: MainModel) {
        val intent = Intent()
        intent.putExtra("url",mainUrl+item.href)
        intent.putExtra("title",item.title)
        intent.setClass(this,ArticleActivity::class.java)
        startActivity(intent)
    }

    lateinit var adapter: CatalogueAdapter
    var mainUrl = "https://www.sanwenji.cn/"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_catalogue)
        val url = intent.getStringExtra("url")
        val title = intent.getStringExtra("title")
        catalogue_title.text = String.format("%s散文集",title)

        catalogue_list.layoutManager = LinearLayoutManager(this)
        adapter = CatalogueAdapter()
        adapter.onClick = this
        catalogue_list.adapter = adapter
        parse(url)
    }
    val handler = Handler {
        when(it.what){
            1 ->{
                val data  = it.obj as  List<MainModel>
                adapter.notifyDataSetChanged(data)
            }

        }
        true
    }
    private fun parse(url: String) {
        NetUtil.request(NetUtil.RequestMethod.GET, url, HashMap(), object : NetUtil.RequestCallBack {
            override fun onSuccess(statusCode: Int, json: String?) {
                val doc = Jsoup.parse(json)
                //获取所有的博客列表集合
                val blogList = doc.select("div[class=list]")
                //解析目录列表
                for(ele in blogList){
                    val authorEle = ele.select("a")
                    val authors = ArrayList<MainModel>()

                    for (au in authorEle){
                        val name = au.text()

                        val href = au.attr("href")
                        if (!TextUtils.isEmpty(href)){
                            val auModel = MainModel(name,href,"")
                            authors.add(auModel)
                        }
                    }
                    //通知页面刷新
                    val msg = Message()
                    msg.what = 1
                    msg.obj = authors
                    handler.sendMessage(msg)
                }
                val page = doc.select("page")
                for (ele in page){
                    val authorEle = ele.select("a")

                    for (au in authorEle){
                        val url = au.attr("href")
                        if (!TextUtils.isEmpty(url)){
                            parseOther(url)
                        }
                    }
                }

            }

            override fun onFailure(statusCode: Int, errorMsg: String?) {

            }

        })
    }
    private fun parseOther(url:String){
        NetUtil.request(NetUtil.RequestMethod.GET, url, HashMap(), object : NetUtil.RequestCallBack {
            override fun onSuccess(statusCode: Int, json: String?) {
                val doc = Jsoup.parse(json)
                //获取所有的博客列表集合
                val blogList = doc.select("div[class=list]")
                //解析目录列表
                for(ele in blogList){
                    val authorEle = ele.select("a")
                    val authors = ArrayList<MainModel>()

                    for (au in authorEle){
                        val name = au.text()

                        val href = au.attr("href")
                        if (!TextUtils.isEmpty(href)){
                            val auModel = MainModel(name,href,"")
                            authors.add(auModel)
                        }
                    }
                    //通知页面刷新
                    val msg = Message()
                    msg.what = 1
                    msg.obj = authors
                    handler.sendMessage(msg)
                }

            }

            override fun onFailure(statusCode: Int, errorMsg: String?) {

            }

        })
    }
}
